package com.yilin.medical.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.activity.KeChengDetailsActivity;
import com.yilin.medical.adapter.KeChengSortAdapter;
import com.yilin.medical.comparator.KeChengPinyinComparator;
import com.yilin.medical.model.KeChengSortBean;
import com.yilin.medical.views.sidebar.CharacterParser;
import com.yilin.medical.views.sidebar.SideBar;
import com.yilin.medical.views.sidebar.SideBar.OnTouchingLetterChangedListener;

/**
 * 内分泌
 * 
 * @author lijianqun
 * 
 */
public class KeChengFragment extends Fragment implements OnClickListener,
		OnTouchingLetterChangedListener, OnItemClickListener {
	private Context context;
	private View mView;

	private ListView mListView;
	private SideBar sideBar;
	private TextView dialog;
	private KeChengSortAdapter adapter;

	private CharacterParser characterParser;
	private List<KeChengSortBean> SourceDateList;
	private KeChengPinyinComparator pinyinComparator;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		context = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = View.inflate(context, R.layout.fragment_kecheng, null);
		initView();
		registerLintener();
		return mView;
	}

	private void initView() {
		// 实例化汉字转拼音类
		characterParser = CharacterParser.getInstance();

		pinyinComparator = new KeChengPinyinComparator();

		sideBar = (SideBar) mView.findViewById(R.id.sidrbar);
		dialog = (TextView) mView.findViewById(R.id.dialog);
		sideBar.setTextView(dialog);

		mListView = (ListView) mView.findViewById(R.id.country_lvcountry);

		SourceDateList = filledData(context.getResources().getStringArray(
				R.array.date));

		// 根据a-z进行排序源数据
		Collections.sort(SourceDateList, pinyinComparator);
		adapter = new KeChengSortAdapter(context, SourceDateList);
		mListView.setAdapter(adapter);
	}



	private List<KeChengSortBean> filledData(String[] date) {
		List<KeChengSortBean> mSortList = new ArrayList<KeChengSortBean>();

		for (int i = 0; i < date.length; i++) {
			KeChengSortBean sortModel = new KeChengSortBean();
			sortModel.setName(date[i]);
			// 汉字转换成拼音
			String pinyin = characterParser.getSelling(date[i]);
			String sortString = pinyin.substring(0, 1).toUpperCase();

			// 正则表达式，判断首字母是否是英文字母
			if (sortString.matches("[A-Z]")) {
				sortModel.setSortLetters(sortString.toUpperCase());
			} else {
				sortModel.setSortLetters("#");
			}

			mSortList.add(sortModel);
		}
		return mSortList;

	}

	// 设置右侧触摸监听
	private void registerLintener() {
		sideBar.setOnTouchingLetterChangedListener(this);
		mListView.setOnItemClickListener(this);

	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public void onTouchingLetterChanged(String s) {

		// 该字母首次出现的位置
		int position = adapter.getPositionForSection(s.charAt(0));
		if (position != -1) {
			mListView.setSelection(position);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// 这里要利用adapter.getItem(position)来获取当前position所对应的对象
		/*Toast.makeText(context,
				((KeChengSortBean) adapter.getItem(position)).getName(),
				Toast.LENGTH_SHORT).show();*/
		Intent intent;
		switch (parent.getId()) {
		case R.id.country_lvcountry:
			intent = new Intent(context,KeChengDetailsActivity.class);
			startActivity(intent);
			break;
		}
	}

}
